﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.Web.Services;
using System.Collections;

namespace Catering.Catering_Page.system
{
    public partial class User_Manage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btn_Add.Attributes.Add("onclick", "return YanZheng();");
                this.btn_Edit.Attributes.Add("onclick", "return YanZheng();");
                this.btn_EditPassWord.Attributes.Add("onclick", "return Edut_YanZheng()");
                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            DataTable dt_zuoyequ = null;
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                int companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
                if (companyid > 0)
                {
                    dt_zuoyequ = new Catering_Rules.OperationArea().GetAllListByCompanyID(companyid, true).Tables[0];
                }
            }
            this.Drop_zuoyequ.LoadDrop(dt_zuoyequ, "id", "AreaName", true);
            this.Drop_zuoyequ_SelectedIndexChanged(null, null);

            DataTable dt_Role = new Catering_Rules.Role().GetAllList().Tables[0];
            this.Drop_Role.LoadDrop(dt_Role, "id", "RoleName");

            this.LoadRep("1=1");
        }

        //加载Rep控件
        public void LoadRep(string where)
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.T_Personnel().GetCountBywhere(where);
            DataTable dt_Personnel = new Catering_Rules.T_Personnel().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, where).Tables[0];
            this.Rep_Personnel.DataSource = dt_Personnel;
            this.Rep_Personnel.DataBind();
        }

        //查询
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            ViewState["page"] = "1";
            string GroupID = this.Drop_zuoyequ.SelectedValue;
            string ClassID = this.Drop_banzu.SelectedValue;
            string name = this.txt_name.Text.Trim();

            string strwhere = "1=1";
            if (GroupID != "-10")
            {
                if (ClassID != "-10")
                {
                    strwhere += " and ClassID=" + ClassID;
                }
                else
                {
                    DataTable dt_Classinfo = new Catering_Rules.ClassInfo().GetList("AreaID=" + GroupID).Tables[0];
                    if (dt_Classinfo != null && dt_Classinfo.Rows.Count > 0)
                    {
                        string idlist = "";
                        foreach (DataRow row in dt_Classinfo.Rows)
                        {
                            idlist += row["id"].ToString() + ",";
                        }
                        strwhere += " and ClassID in (" + idlist.TrimEnd(',') + ")";
                    }
                    else
                    {
                        strwhere += " and 1=2";
                    }
                }
            }

            if (name != "")
            {
                strwhere += " and Name like '%" + name + "%'";
            }

            this.LoadRep(strwhere);
        }

        //判定该人员是否存在用户信息
        public string IsExistsUser(string uid)
        {
            if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "System_power.aspx", "用户管理操作"))
            {
                DataTable dt_user = new Catering_Rules.user().GetList("PersonnelID=" + uid).Tables[0];
                if (dt_user != null && dt_user.Rows.Count > 0)
                {
                    return "<a href='javascript:void(0);' id=\"btnEdit\" onclick=\"ShowEdit('" + uid + "')\">编辑用户</a>　<a href='javascript:void(0);' id=\"btnEdit\" onclick=\"ShowEditPassWord('" + uid + "')\">修改密码</a>";
                }
                else
                {
                    return "<a href='javascript:void(0);' id=\"btnAdd\" onclick=\"ShowAdd('" + uid + "')\" >添加用户</a>";
                }
            }
            else
            {

                return "";
            }
        }

        //获取班组名称
        public string GetClassName(string uid)
        {
            if (uid == "")
                return "";

            Catering_Model.ClassInfo md = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(uid));
            return md == null ? "" : md.CName;
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.ANPSerach();
        }

        //分页查询
        public void ANPSerach()
        {
            if (ViewState["page"] != null)
            {
                this.btn_Search_Click(null, null);
            }
            else
            {
                this.LoadRep("1=1");
            }
        }

        //加载班组
        protected void Drop_zuoyequ_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_banzu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_zuoyequ.SelectedValue).Tables[0];
            this.Drop_banzu.LoadDrop(dt_banzu, "id", "CName", true);
        }

        //添加用户
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Catering_Model.user md = new Catering_Model.user();
            md.id = new Catering_Rules.user().GetMaxId();
            md.UName = this.txt_username.Text.Trim();
            md.UPWD = this.txt_password.Text.Trim().GetMD5("");
            md.URole = Convert.ToInt32(this.Drop_Role.SelectedValue);
            md.QQ = this.txt_QQ.Text.Trim();
            md.Tel = this.txt_tel.Text.Trim();
            md.Email = this.txt_Email.Text.Trim();
            md.PersonnelID = Convert.ToInt32(this.HidRid.Value);
            new Catering_Rules.user().Add(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！')</script>");
            this.ANPSerach();
        }

        //修改用户
        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            Catering_Model.user md = new Catering_Model.user();
            md.id = Convert.ToInt32(this.HidRid.Value);
            md.UName = this.txt_username.Text.Trim();
            md.UPWD = new Catering_Rules.user().GetModel(md.id).UPWD;//this.txt_password.Text.Trim().GetMD5("");
            md.URole = Convert.ToInt32(this.Drop_Role.SelectedValue);
            md.QQ = this.txt_QQ.Text.Trim();
            md.Tel = this.txt_tel.Text.Trim();
            md.Email = this.txt_Email.Text.Trim();
            md.PersonnelID = new Catering_Rules.user().GetModel(Convert.ToInt32(md.id)).PersonnelID;
            new Catering_Rules.user().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！')</script>");
            this.ANPSerach();
        }

        //修改密码
        protected void btn_EditPassWord_Click(object sender, EventArgs e)
        {
            Catering_Model.user md_old = new Catering_Rules.user().GetModel(Convert.ToInt32(this.HidRid.Value));
            Catering_Model.user md = new Catering_Model.user();
            md.id = Convert.ToInt32(this.HidRid.Value);
            md.UName = md_old.UName;
            md.UPWD = this.txt_Editpassword.Text.Trim().GetMD5("");
            md.URole = md_old.URole;
            md.QQ = md_old.QQ;
            md.Tel = md_old.Tel;
            md.Email = md_old.Email;
            md.PersonnelID = md_old.PersonnelID;
            new Catering_Rules.user().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！')</script>");
            this.ANPSerach();
        }

        //获取人员信息
        [WebMethod]
        public static ArrayList GetPersonnel(string uid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.E_Personnel md = new Catering_Rules.T_Personnel().GetModel(Convert.ToInt32(uid));
            string qq = md.QQ;
            string Tel = md.Tel;
            string Email = md.Email;
            if (string.IsNullOrEmpty(qq) || qq.ToLower() == "null") qq = "";
            if (string.IsNullOrEmpty(Tel) || Tel.ToLower() == "null") Tel = "";
            if (string.IsNullOrEmpty(Email) || Email.ToLower() == "null") Email = "";
            al.Add(qq);
            al.Add(Tel);
            al.Add(Email);
            return al;
        }

        //获取用户信息
        [WebMethod]
        public static ArrayList GetUser(string uid)
        {
            ArrayList al = new ArrayList();
            DataTable dt_User = new Catering_Rules.user().GetList("PersonnelID=" + uid).Tables[0];
            string qq = dt_User.Rows[0]["QQ"].ToString();
            string Tel = dt_User.Rows[0]["Tel"].ToString();
            string Email = dt_User.Rows[0]["Email"].ToString();
            if (string.IsNullOrEmpty(qq) || qq.ToLower() == "null") qq = "";
            if (string.IsNullOrEmpty(Tel) || Tel.ToLower() == "null") Tel = "";
            if (string.IsNullOrEmpty(Email) || Email.ToLower() == "null") Email = "";
            al.Add(dt_User.Rows[0]["UName"].ToString());
            al.Add(dt_User.Rows[0]["UPWD"].ToString());
            al.Add(dt_User.Rows[0]["URole"].ToString());
            al.Add(qq);
            al.Add(Tel);
            al.Add(Email);
            al.Add(dt_User.Rows[0]["id"].ToString());
            return al;
        }

        //验证用户名是否存在
        [WebMethod]
        public static ArrayList UserIsExts(string uname, string IsAdd, string uid)
        {
            ArrayList al = new ArrayList();
            if (IsAdd == "Add")
            {
                DataTable dt_user = new Catering_Rules.user().GetList("UName='" + uname + "'").Tables[0];
                al.Add((dt_user != null && dt_user.Rows.Count > 0) ? "NO" : "OK");
            }
            else
            {
                string strwhere = "UName='" + uname + "' and id not in(" + uid + ")";
                DataTable dt_user = new Catering_Rules.user().GetList(strwhere).Tables[0];
                al.Add((dt_user != null && dt_user.Rows.Count > 0) ? "NO" : "OK");
            }
            return al;
        }

        //获取用户名
        [WebMethod]
        public static string GetUserName(string uid)
        {
            //return new Catering_Rules.user().GetModel(Convert.ToInt32(uid)).UName;
            DataTable dt_User = new Catering_Rules.user().GetList("PersonnelID=" + uid).Tables[0];
            if (dt_User != null && dt_User.Rows[0]["UName"].ToString() != "") return dt_User.Rows[0]["UName"].ToString();
            return "";

        }

        public string GetBranchPost(string pid)
        {
            int id = 0;
            Int32.TryParse(pid, out id);
            if (id > 0)
            {
                Catering_Model.BranchPost bp = new Catering_Rules.BranchPost().GetModel(id);
                if (bp != null && !string.IsNullOrEmpty(bp.Name)) return bp.Name;
            }
            return "&nbsp;";
        }

        public string GetOldByDate(string strDate)
        {
            DateTime date = DateTime.MinValue;
            DateTime.TryParse(strDate, out date);
            if (!string.IsNullOrEmpty(date.ToString()) && date.ToShortDateString() != "1900/1/1")
            {
                return (DateTime.Now.Year - date.Year).ToString();
            }
            return "";
        }

        public string GetPersonnelInfo(string StrID)
        {
            DataTable dt_user = new Catering_Rules.user().GetList("PersonnelID=" + StrID).Tables[0];
            if (dt_user != null && dt_user.Rows.Count > 0)
            {
                return dt_user.Rows[0]["UName"].ToString();
            }
            else
            {
                return "";
            }
        }
    }
}